package com.test.schoolhelper.ui.activity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.test.schoolhelper.R;
import com.test.schoolhelper.base.BaseActivity;
import com.test.schoolhelper.databinding.ActivityAddAlarmBinding;
import com.test.schoolhelper.utils.AlarmUtils;
import com.test.schoolhelper.utils.CalendarReminderUtils;
import com.test.schoolhelper.utils.ToastUtils;
import com.test.schoolhelper.utils.Tools;

import java.util.Calendar;
import java.util.List;

// TODO: 2024/5/31 添加闹钟
public class AddAlarmActivity extends BaseActivity<ActivityAddAlarmBinding> implements View.OnClickListener {

    private String time;

    @Override
    protected void initClick() {
        super.initClick();
        getBinding().ivBack.setOnClickListener(this);
        getBinding().rlTime.setOnClickListener(this);
        getBinding().tvSave.setOnClickListener(this);
    }


    // TODO:设置开始/结束时间
    private void showDateDialog() {
        //时间选择器
        TimePickerView pvTime = new TimePickerBuilder(this, (date, v) -> {
            time = Tools.customFormat(date, "H:m");
            getBinding().tvTime.setText(time);

        }).setType(new boolean[]{false, false, false, true, true, false})//控制年-月-日-时-分-秒
                .build();
        pvTime.show();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_add_alarm;
    }

    @Override
    protected ActivityAddAlarmBinding onCreateViewBinding(@NonNull LayoutInflater layoutInflater) {
        return ActivityAddAlarmBinding.inflate(layoutInflater);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_time:
                showDateDialog();
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_save:
                handler.sendEmptyMessage(0);
                break;
        }
    }

    private void save() {
        String name = getBinding().etName.getText().toString();
        if (name.isEmpty()) {
            ToastUtils.getInstance().show(this, "请输入名称");
            return;
        }
        String[] times = time.split(":");
        int hour = Integer.parseInt(times[0]);
        int minute = Integer.parseInt(times[1]);
        AlarmUtils.createAlarm(this, name, hour, minute, 0);
        new Handler().postDelayed(() -> {
            finish();
        }, 500);
    }


    Handler handler = new Handler(msg -> {
        switch (msg.what) {
            case 0:
                save();
                break;
        }
        return false;
    });

}